function needLogin() {
	uni.showModal({
		title: '提示',
		content: '需要登录才能进行操作？',
		success: res => {
			if (res.confirm) {
				uni.navigateTo({
					url: '/pages/account/main',
					animationType: 'slide-in-right',
					animationDuration: 300
				})
			} else if (res.cancel) {
				uni.navigateBack()
			}
		}
	})
}

const install = (Vue, vm) => {
	// 全局配置
	Vue.prototype.$u.http.setConfig({
		baseUrl: 'https://www.supercloudvip.com/api',
		showLoading: false,
		loadingMask: false,
		originalData: true
	});

	// 请求拦截
	Vue.prototype.$u.http.interceptor.request = (config) => {
		let token = vm.$store.state.token
		if (token) {
			config.header.Authorization = 'Bearer ' + token
		} else {
			config.header.Authorization = ''
		}
		return config;
	}

	// 响应拦截
	Vue.prototype.$u.http.interceptor.response = (res) => {
		if (res.data.success) {
			return res
		} else {
			if (vm.$store.state.token) {
				if (res.statusCode == 401) {
					needLogin();
				} else {
					uni.showToast({
						title: '操作频繁或者网络开小差了！',
						icon: 'none'
					})
				}
			} else {
				needLogin();
			}
			console.log('fail', res)
			return res
		}
	}
	
	// 结束
}

export default {
	install
}
